﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace HGZP.PLL
{
    public class BaseConfig
    {
        /// <summary>
        /// 读netcms.config取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <param name="ConfigPathName"></param>
        /// <returns></returns>
       public  static string GetConfigValue(string Target, string XmlPath)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            return elemList[0].InnerXml;
        }

        /// <summary>
        /// web.config相关文件操作
        /// 0检测是web.config是否为只读或可写;返回值为:true或false,1把config改写为只读;2把web.config改写为可写
        /// 在此函数中自动去根目下寻找web.config
        /// </summary>
        /// <param name="flg">0检测是web.config是否为只读或可写;返回值为:true或false,1把web.config改写为只读;2把web.config改写为可写</param>
        /// 2007-5-9 y.xiaobin
        /// <returns></returns>
        public static bool constReadOnly(int num, string strSource)
        {
            bool _readonly = false;
            string _config = HttpContext.Current.Server.MapPath(@"~/" + strSource);
            FileInfo fi = new FileInfo(_config);
            switch (num)
            {
                case 0: _readonly = fi.IsReadOnly; break;
                case 1:
                    fi.IsReadOnly = true;
                    _readonly = true;
                    break;
                case 2:
                    {
                        fi.IsReadOnly = false;
                        _readonly = false;
                    }
                    break;
                default: throw new Exception("错误参数!");
            }

            return _readonly;

        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="strTarget">接点名</param>
        /// <returns></returns>
        public static void SaveXmlConfig(string strTarget, string strValue, string strSource)
        {
            string xmlPath = HttpContext.Current.Server.MapPath("~/" + strSource);
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(strTarget);
            elemList[0].InnerXml = strValue;
            xdoc.Save(xmlPath);
        }
    }
}
